//package com.lagou.java;
//
//import org.apache.curator.RetryPolicy;
//import org.apache.curator.framework.CuratorFramework;
//import org.apache.curator.framework.CuratorFrameworkFactory;
//import org.apache.curator.framework.api.CuratorEvent;
//import org.apache.curator.framework.api.CuratorListener;
//import org.apache.curator.framework.recipes.cache.NodeCache;
//import org.apache.curator.framework.recipes.cache.NodeCacheListener;
//import org.apache.curator.retry.ExponentialBackoffRetry;
//import org.apache.zookeeper.CreateMode;
//import org.apache.zookeeper.WatchedEvent;
//import org.apache.zookeeper.Watcher;
//import sun.misc.Cleaner;
//
//import java.util.List;
//
//public class CuratorTest {
//
//    private static String basePath = "/root";
//
//    public static void main(String[] args) throws Exception {
//        CuratorFramework client = new CuratorTest().createWithOptions("127.0.0.1:2181",
//                new ExponentialBackoffRetry(1000, 3), 5000, 5000);
//        client.start();
//        create(client, "", "");
//        List<String> strings = watchedGetChildren(client, basePath, new Watcher() {
//            @Override
//            public void process(WatchedEvent watchedEvent) {
//                System.out.println("===============");
//                System.out.println(watchedEvent);
//            }
//        });
//        listenNode(client, basePath);
//        create(client, "adfadf", "dafsfasf");
//        create(client, "adfadf", "dafsfasf");
//        create(client, "adfadf", "dafsfasf");
//        create(client, "adfadf", "dafsfasf");
//        System.out.println(">>>>>>>>>>>>>>>>>>"+ strings);
//        Thread.sleep(Integer.MAX_VALUE);
//
//    }
//
//
//    public static CuratorFramework  createWithOptions(String connectionString, RetryPolicy retryPolicy, int connectionTimeoutMs, int sessionTimeoutMs)
//    {
//        // using the CuratorFrameworkFactory.builder() gives fine grained control
//        // over creation options. See the CuratorFrameworkFactory.Builder javadoc
//        // details
//        return CuratorFrameworkFactory.builder()
//                .connectString(connectionString)
//                .retryPolicy(retryPolicy)
//                .connectionTimeoutMs(connectionTimeoutMs)
//                .sessionTimeoutMs(sessionTimeoutMs)
//                .build();
//    }
//
//    public static List<String> watchedGetChildren(CuratorFramework client, String path, Watcher watcher) throws Exception
//    {
//
//        CuratorListener listener = new CuratorListener()
//        {
//            @Override
//            public void eventReceived(CuratorFramework client, CuratorEvent event) throws Exception
//            {
//                System.out.println("===============" + event);
//                CuratorListener listener = new CuratorListener(){
//                    @Override
//                    public void eventReceived(CuratorFramework client, CuratorEvent event) throws Exception
//                    {
//                        System.out.println("===============" + event);
//                    };
//                };
//                client.getCuratorListenable().addListener(listener);
//                // examine event for details
//            }
//        };
//        client.getCuratorListenable().addListener(listener);
//        /**
//         * Get children and set the given watcher on the node.
//         */
//        return client.getChildren().inBackground().forPath(path);
//
//
//    }
//
//    public static void create(CuratorFramework client, String path, String data) throws Exception {
//        client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath(basePath + "/hah");
//    }
//
//    public static void setData(CuratorFramework client, String path, byte[] payload) throws Exception
//    {
//        // set data for the given node
//        client.setData().forPath(path, payload);
//    }
//
//    public static void listenNode (CuratorFramework client, String path) throws Exception{
//        final NodeCache nodeCache = new NodeCache(client,path);
//        nodeCache.start();
//        nodeCache.getListenable().addListener(new NodeCacheListener() {
//            @Override
//            public void nodeChanged() throws Exception {
//                System.out.println("监听事件触发");
//                System.out.println("重新获得节点内容为：" + new String(nodeCache.getCurrentData().getData()));
//            }
//        });
//
//    }
//}
